package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;
    @Log
    @RequestMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门；" + dept);
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping//查询部门列表
    public Result list(String name, Integer status,
                      @RequestParam (defaultValue = "1")Integer page,
                      @RequestParam (defaultValue = "10")Integer pageSize){
        log.info("列表查询；" + name + " " + status + " " + page + " " + pageSize);
        PageResult<Dept> pageResult = deptService.list(name, status, page, pageSize);
                return Result.success(pageResult) ;

    }

    @GetMapping("/{id}")//获取部门信息
    public Result getDeptById(@PathVariable Integer id){
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }
    @Log
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("修改部门：" + dept);
        deptService.updateDept(dept);
        return Result.success();
    }
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除部门：" + id);
        deptService.deleteDept(id);
        return Result.success();
    }

    //查询全部部门
    @GetMapping("/list")
    public Result list(){
        log.info("查询全部部门");
        return Result.success(deptService.listAll( 1));
    }



}
